<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;


/**
 * Class Articles
 *
 * @package App\Model
 * @property-read \App\Model\ArticleCategories $articleCategory
 * @property-read mixed $sort_desc
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Articles onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Articles withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Articles withoutTrashed()
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles query()
 * @property int $id 主键id
 * @property int $author media_user->id 作者0为系统
 * @property string|null $title 标题
 * @property string|null $sub_title 副标题
 * @property string|null $label_id 标签
 * @property string|null $content 正文
 * @property string|null $cover 封面图片地址
 * @property int|null $advertisement_id 广告id
 * @property int $is_ad
 * @property int|null $article_category_id 文章分类id
 * @property int|null $sort 排序
 * @property int|null $creator 创建人uid
 * @property int|null $pageviews 浏览量
 * @property int|null $actual_pageviews 实际浏览量
 * @property int $comment_num 评论数量
 * @property string|null $last_comment_at 最后评论时间
 * @property int|null $status 审核状态 1审核中， 2 已发布， 3未通过， 4 草稿，5 已撤回
 * @property int $share_num 分享数量
 * @property int $recommend_num 推荐数量
 * @property int $collect_num 收藏数量
 * @property int $operate_status 发布撤回状态1 没有撤回 ，2 撤回
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 修改时间
 * @property int|null $is_deleted 是否删除
 * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
 * @property int $thumb 点赞
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereActualPageviews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereAdvertisementId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereArticleCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereAuthor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereCollectNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereCommentNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereCover($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereCreator($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereIsAd($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereIsDeleted($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereLabelId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereLastCommentAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereOperateStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles wherePageviews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereRecommendNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereShareNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereSubTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Articles whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Model\UserInterPushRecordMaterial[] $interPushRecordMaterial
 * @property-read \App\Model\UserInterPushInfo $interPushInfo
 * @property-read \App\Model\MediaUserInfo $author_info
 */
class Articles extends Model
{
    use SoftDeletes;

    protected $casts = [
        'article_category_id' => 'int',
        'id' => 'int'
    ];
    protected  $table = 'articles';

    protected $primaryKey = 'id';

    protected $dates = ['deleted_at'];

    protected $fillable = ['id','title', 'sort'];

    protected $appends = ['sort_desc'];

    public function articleCategory()
    {
        return $this->belongsTo(ArticleCategories::class, 'article_category_id', 'id');
    }


    public function add(Request $request)
    {
        $data = $request->input();

        $this->title = $data['title'];
        $this->content = $data['content'];
        $this->cover = $data['cover'];
        $this->article_category_id = $data['article_category_id'];
        $this->creator = $data['creator'];
        $this->pageviews = $data['pageviews'];
        return $this->save();
    }

    public function getSortDescAttribute()
    {
        return strip_tags($this->content);
    }

    public function getContent()
    {
        return $this->hasMany(Comment::class,'a_id', 'id');
    }

    /**
     *  获取文章互推匹配记录
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function interPushRecordMaterial()
    {
        return $this->morphedByMany(UserInterPushRecordMaterial::class, "material","source_type","source_id","id");
    }


    /**
     * 文章对应素材
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function interPushInfo()
    {
        return $this->morphedByMany(UserInterPushInfo::class,"material","material_type","material_id","id");
    }

    public function author_info()
    {
        return $this->hasOne(MediaUserInfo::class, 'media_user_id', 'author');
    }

}
